package com.wll.ppjoke.ui.find;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.LabeledIntent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import com.wll.libnavannotation.FragmentDestination;
import com.wll.ppjoke.R;
import com.wll.ppjoke.model.SofaTab;
import com.wll.ppjoke.ui.sofa.SofaFragment;
import com.wll.ppjoke.utils.AppConfig;

import java.util.ArrayList;
import java.util.List;

@FragmentDestination(pageUrl = "main/tabs/find", asStarter = false)
public class FindFragment extends SofaFragment/*extends Fragment*/ {
    private static final String TAG = "FindFragment";

    @Override
    public Fragment getTabFragment(int position) {
        SofaTab.Tabs tab = getTabConfig().tabs.get(position);
        return TagListFragment.newInstance(tab.tag);
    }

    @Override
    public void onAttachFragment(@NonNull Fragment childFragment) {
        super.onAttachFragment(childFragment);
        String tagType = null;
        if (childFragment.getArguments() != null) {
            tagType = childFragment.getArguments().getString(TagListFragment.KEY_TAG_TYPE);
            if (TextUtils.equals(tagType, "onlyFollow")) {
                TagListViewModel tagListViewModel = new ViewModelProvider(this).get(TagListViewModel.class);
//            ViewModelProviders.of(childFragment).get(TagListViewModel.class)
//                    .getSwitchTabLiveData().observe(this, object -> viewPager2.setCurrentItem(1));
                tagListViewModel.getSwitchTabLiveData().observe(this, new Observer<Object>() {
                    @Override
                    public void onChanged(Object o) {
                        viewPager2.setCurrentItem(1);
                    }
                });
            }
        }
    }

    @Override
    public SofaTab getTabConfig() {
        return AppConfig.getFindTabConfig();
    }

//    private FindViewModel findViewModel;
//
//    public View onCreateView(@NonNull LayoutInflater inflater,
//                             ViewGroup container, Bundle savedInstanceState) {
//        Log.e(TAG, "onCreateView");
//        findViewModel =
//                ViewModelProviders.of(this).get(FindViewModel.class);
//        View root = inflater.inflate(R.layout.fragment_find, container, false);
//        final TextView textView = root.findViewById(R.id.text_home);
//        findViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                textView.setText(s);
//            }
//        });
//
//        textView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//                intent.setType("image/*");
//                startActivityForResult(intent, 1);
//            }
//        });
//        return root;
//
//    }
//
//    @Override
//    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        Uri uri = data.getData();
//        share(uri);
//    }

//    public void share(Uri uri) {
////       Intent intent = new Intent();
////       intent.setAction(Intent.ACTION_SEND);
////       intent.putExtra(Intent.EXTRA_TEXT,"文字分享");
////       intent.setType("text/plain");
////       startActivity(Intent.createChooser(intent,"选择分享应用"));
//
////       Intent intent = new Intent();
////       intent.setAction(Intent.ACTION_SEND);
////       intent.setType("text/plain");
////       Uri uri = Uri.parse("file://" + getContext().getExternalCacheDir());
//        Intent intent = new Intent();
//        intent.setAction(Intent.ACTION_SEND);
////        intent.putExtra(Intent.EXTRA_STREAM, uri);
//        intent.setType("image/*");
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
////       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//        PackageManager pm = getContext().getPackageManager();
//        List<ResolveInfo> resInfo = pm.queryIntentActivities(intent, 0);
//        if (resInfo.isEmpty()) {
//            Log.e("分享", "没有可以分享的应用");
//            return;
//        }
//        List<Intent> targetIntents = new ArrayList<>();
//        for (ResolveInfo resolveInfo : resInfo) {
//            ActivityInfo activityInfo = resolveInfo.activityInfo;
//            Log.e("分享", activityInfo.packageName + "  " + activityInfo.name);
//            if (activityInfo.packageName.contains("com.tencent.mm")
//                    || activityInfo.packageName.contains("com.tencent.mobileqq")) {
//                //过滤掉qq收藏
//                if (resolveInfo.loadLabel(pm).toString().contains("QQ收藏")) {
//                    continue;
//                }
//                Intent target = new Intent();
//                target.setAction(Intent.ACTION_SEND);
//                target.setComponent(new ComponentName(activityInfo.packageName, activityInfo.name));
//
//                target.putExtra(Intent.EXTRA_STREAM, uri);
//                target.setType("image/*");//必须设置，否则选定分享类型后不能跳转界面
//                targetIntents.add(new LabeledIntent(target, activityInfo.packageName, resolveInfo.loadLabel(pm), resolveInfo.icon));
////                targetIntents.add(target);
//            }
//        }
//        if (targetIntents.size() <= 0) {
//            Log.e("分享", "没有可以分享的应用");
//            return;
//        }
////        Intent chooser = Intent.createChooser(targetIntents.remove(targetIntents.size() - 1), "选择分享");
//        Intent chooser = Intent.createChooser(targetIntents.get(targetIntents.size() - 1), "选择分享");
//        if (chooser == null) return;
//        LabeledIntent[] labeledIntents = targetIntents.toArray(new LabeledIntent[targetIntents.size()]);
////        Intent[] labeledIntents = targetIntents.toArray(new Intent[targetIntents.size()]);
//        chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, labeledIntents);
//        startActivity(chooser);
//    }

}
